﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace UmlDesigner.Model.WorkUnits
{
    class MoveItemsWorkUnit : IWorkUnit
    {
        List<ItemWidget> items = new List<ItemWidget>();

        Point oldPosition;
        Point newPosition;

        internal MoveItemsWorkUnit(List<ItemWidget> itemsToMove, Point oldPos)
        {
            items.InsertRange(0, itemsToMove);
            oldPosition = oldPos;
        }

        internal Point Position
        {
            set
            {
                newPosition = value;
            }
        }

        bool alreadyApplied = true;

        #region IWorkUnit Members

        public void Do(UmlDesigner.Gui.UmlControl umlControl)
        {
            if (!alreadyApplied)
            {
                int dx = newPosition.X - oldPosition.X;
                int dy = newPosition.Y - oldPosition.Y;
                MoveItems(dx, dy);
            }
        }

        public void Undo(UmlDesigner.Gui.UmlControl umlControl)
        {
            alreadyApplied = false;
            int dx = oldPosition.X - newPosition.X;
            int dy = oldPosition.Y - newPosition.Y;
            MoveItems(dx, dy);
        }
       
        private void MoveItems(int dx, int dy)
        {

            foreach (ItemWidget item in items)
            {
                item.SetClassPosition(new Point(item.Position.X + dx, item.Position.Y + dy));
                if (dx % 2 == 0 || dy % 2 == 0)
                {
                    item.RecalculateAllSlots();
                }
            }
        }

 #endregion
    }
}
